package com.example.demo.controller;

import com.example.demo.config.Interceptor.PassToken;
import com.example.demo.dao.TokenDao;
import com.example.demo.dao.User;
import com.example.demo.service.impl.Userserviceimpl;
import com.example.demo.utils.CommonRetumType;
import com.example.demo.utils.JwtUtils;
import com.example.demo.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class loginController extends BaseController{
    @Autowired
    Userserviceimpl userserviceimpl;
    @Autowired
    RedisUtil redisUtil;

    @PassToken
    @PostMapping("/login")
    public CommonRetumType login(@RequestBody User user)
    {
        HashMap<String,String> site= new HashMap<String,String>();
        User user1 = userserviceimpl.login(user.getUsername(), user.getPassword());
        String token = JwtUtils.createToken(user1.getId().toString(),user1.getRoleId().toString(),user1.getUsername(),user.getPassword());

        TokenDao tokenDao = new TokenDao();
        tokenDao.setToken(token);
        redisUtil.set("token",token);
        site.put("token",token);
        return CommonRetumType.create(site);
    }
}
